In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
from prettypandas import PrettyPandas
In [3]:
import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
axis=1)
In [4]:
df
Out[4]:
A
B
C
D
E
0
1.0
1.329212
-0.770033
-0.316280
-0.990810
1
2.0
-1.070816
-1.438713
0.564417
0.295722
2
3.0
-1.626404
0.219565
0.678805
1.889273
3
4.0
0.961538
0.104011
-0.481165
0.850229
4
5.0
1.453425
1.057737
0.165562
0.515018
5
6.0
-1.336936
0.562861
1.392855
-0.063328
6
7.0
0.121668
1.207603
-0.002040
1.627796
7
8.0
0.354493
1.037528
-0.385684
0.519818
8
9.0
1.686583
-1.325963
1.428984
-2.089354
9
10.0
-0.129820
0.631523
-0.586538
0.290720
In [5]:
df.pipe(PrettyPandas).total(axis=None)
Out[5]:
A
B
C
D
E
Total
0
1
1.32921
-0.770033
-0.31628
-0.99081
0.252088
1
2
-1.07082
-1.43871
0.564417
0.295722
0.350609
2
3
-1.6264
0.219565
0.678805
1.88927
4.16124
3
4
0.961538
0.104011
-0.481165
0.850229
5.43461
4
5
1.45342
1.05774
0.165562
0.515018
8.19174
5
6
-1.33694
0.562861
1.39285
-0.063328
6.55545
6
7
0.121668
1.2076
-0.00204021
1.6278
9.95503
7
8
0.354493
1.03753
-0.385684
0.519818
9.52615
8
9
1.68658
-1.32596
1.42898
-2.08935
8.70025
9
10
-0.12982
0.631523
-0.586538
0.29072
10.2059
Total
55
1.74294
1.28612
2.45891
2.84508
In [6]:
PrettyPandas(df).total()
Out[6]:
A
B
C
D
E
0
1
1.32921
-0.770033
-0.31628
-0.99081
1
2
-1.07082
-1.43871
0.564417
0.295722
2
3
-1.6264
0.219565
0.678805
1.88927
3
4
0.961538
0.104011
-0.481165
0.850229
4
5
1.45342
1.05774
0.165562
0.515018
5
6
-1.33694
0.562861
1.39285
-0.063328
6
7
0.121668
1.2076
-0.00204021
1.6278
7
8
0.354493
1.03753
-0.385684
0.519818
8
9
1.68658
-1.32596
1.42898
-2.08935
9
10
-0.12982
0.631523
-0.586538
0.29072
Total
55
1.74294
1.28612
2.45891
2.84508
In [7]:
PrettyPandas(df).average()
Out[7]:
A
B
C
D
E
0
1
1.32921
-0.770033
-0.31628
-0.99081
1
2
-1.07082
-1.43871
0.564417
0.295722
2
3
-1.6264
0.219565
0.678805
1.88927
3
4
0.961538
0.104011
-0.481165
0.850229
4
5
1.45342
1.05774
0.165562
0.515018
5
6
-1.33694
0.562861
1.39285
-0.063328
6
7
0.121668
1.2076
-0.00204021
1.6278
7
8
0.354493
1.03753
-0.385684
0.519818
8
9
1.68658
-1.32596
1.42898
-2.08935
9
10
-0.12982
0.631523
-0.586538
0.29072
Average
5.5
0.174294
0.128612
0.245891
0.284508
In [8]:
PrettyPandas(df).average(axis=1)
Out[8]:
A
B
C
D
E
Average
0
1
1.32921
-0.770033
-0.31628
-0.99081
0.0504176
1
2
-1.07082
-1.43871
0.564417
0.295722
0.0701218
2
3
-1.6264
0.219565
0.678805
1.88927
0.832248
3
4
0.961538
0.104011
-0.481165
0.850229
1.08692
4
5
1.45342
1.05774
0.165562
0.515018
1.63835
5
6
-1.33694
0.562861
1.39285
-0.063328
1.31109
6
7
0.121668
1.2076
-0.00204021
1.6278
1.99101
7
8
0.354493
1.03753
-0.385684
0.519818
1.90523
8
9
1.68658
-1.32596
1.42898
-2.08935
1.74005
9
10
-0.12982
0.631523
-0.586538
0.29072
2.04118
In [9]:
PrettyPandas(df).average(axis=None)
Out[9]:
A
B
C
D
E
Average
0
1
1.32921
-0.770033
-0.31628
-0.99081
0.0504176
1
2
-1.07082
-1.43871
0.564417
0.295722
0.0701218
2
3
-1.6264
0.219565
0.678805
1.88927
0.832248
3
4
0.961538
0.104011
-0.481165
0.850229
1.08692
4
5
1.45342
1.05774
0.165562
0.515018
1.63835
5
6
-1.33694
0.562861
1.39285
-0.063328
1.31109
6
7
0.121668
1.2076
-0.00204021
1.6278
1.99101
7
8
0.354493
1.03753
-0.385684
0.519818
1.90523
8
9
1.68658
-1.32596
1.42898
-2.08935
1.74005
9
10
-0.12982
0.631523
-0.586538
0.29072
2.04118
Average
5.5
0.174294
0.128612
0.245891
0.284508
In [10]:
PrettyPandas(df).min().max()
Out[10]:
A
B
C
D
E
0
1
1.32921
-0.770033
-0.31628
-0.99081
1
2
-1.07082
-1.43871
0.564417
0.295722
2
3
-1.6264
0.219565
0.678805
1.88927
3
4
0.961538
0.104011
-0.481165
0.850229
4
5
1.45342
1.05774
0.165562
0.515018
5
6
-1.33694
0.562861
1.39285
-0.063328
6
7
0.121668
1.2076
-0.00204021
1.6278
7
8
0.354493
1.03753
-0.385684
0.519818
8
9
1.68658
-1.32596
1.42898
-2.08935
9
10
-0.12982
0.631523
-0.586538
0.29072
Minimum
1
-1.6264
-1.43871
-0.586538
-2.08935
Maximum
10
1.68658
1.2076
1.42898
1.88927
In [11]:
PrettyPandas(df).summary(np.mean, "Average")
Out[11]:
A
B
C
D
E
0
1
1.32921
-0.770033
-0.31628
-0.99081
1
2
-1.07082
-1.43871
0.564417
0.295722
2
3
-1.6264
0.219565
0.678805
1.88927
3
4
0.961538
0.104011
-0.481165
0.850229
4
5
1.45342
1.05774
0.165562
0.515018
5
6
-1.33694
0.562861
1.39285
-0.063328
6
7
0.121668
1.2076
-0.00204021
1.6278
7
8
0.354493
1.03753
-0.385684
0.519818
8
9
1.68658
-1.32596
1.42898
-2.08935
9
10
-0.12982
0.631523
-0.586538
0.29072
Average
5.5
0.174294
0.128612
0.245891
0.284508
In [36]:
def count_greater_than_zero(column):
return (column > 0).sum()
PrettyPandas(df).summary(count_greater_than_zero, title="> 0")
Out[36]:
A
B
C
D
E
0
1
1.32921
-0.770033
-0.31628
-0.99081
1
2
-1.07082
-1.43871
0.564417
0.295722
2
3
-1.6264
0.219565
0.678805
1.88927
3
4
0.961538
0.104011
-0.481165
0.850229
4
5
1.45342
1.05774
0.165562
0.515018
5
6
-1.33694
0.562861
1.39285
-0.063328
6
7
0.121668
1.2076
-0.00204021
1.6278
7
8
0.354493
1.03753
-0.385684
0.519818
8
9
1.68658
-1.32596
1.42898
-2.08935
9
10
-0.12982
0.631523
-0.586538
0.29072
> 0
10
6
7
5
7
In [13]:
PrettyPandas(df).multi_summary([np.mean, np.sum],
['Average', 'Total'],
axis=None)
Out[13]:
A
B
C
D
E
Average
Total
0
1
1.32921
-0.770033
-0.31628
-0.99081
0.0504176
0.252088
1
2
-1.07082
-1.43871
0.564417
0.295722
0.0701218
0.350609
2
3
-1.6264
0.219565
0.678805
1.88927
0.832248
4.16124
3
4
0.961538
0.104011
-0.481165
0.850229
1.08692
5.43461
4
5
1.45342
1.05774
0.165562
0.515018
1.63835
8.19174
5
6
-1.33694
0.562861
1.39285
-0.063328
1.31109
6.55545
6
7
0.121668
1.2076
-0.00204021
1.6278
1.99101
9.95503
7
8
0.354493
1.03753
-0.385684
0.519818
1.90523
9.52615
8
9
1.68658
-1.32596
1.42898
-2.08935
1.74005
8.70025
9
10
-0.12982
0.631523
-0.586538
0.29072
2.04118
10.2059
Average
5.5
0.174294
0.128612
0.245891
0.284508
Total
55
1.74294
1.28612
2.45891
2.84508
In [15]:
from prettypandas import as_percent, as_currency, as_unit
In [16]:
df.style.format(as_percent(), subset='E')
Out[16]:
A
B
C
D
E
0
1
1.32921
-0.770033
-0.31628
-99.08%
1
2
-1.07082
-1.43871
0.564417
29.57%
2
3
-1.6264
0.219565
0.678805
188.93%
3
4
0.961538
0.104011
-0.481165
85.02%
4
5
1.45342
1.05774
0.165562
51.50%
5
6
-1.33694
0.562861
1.39285
-6.33%
6
7
0.121668
1.2076
-0.00204021
162.78%
7
8
0.354493
1.03753
-0.385684
51.98%
8
9
1.68658
-1.32596
1.42898
-208.94%
9
10
-0.12982
0.631523
-0.586538
29.07%
In [37]:
PrettyPandas(df).as_percent(subset='A') # Format just column A
Out[37]:
A
B
C
D
E
0
100.00%
1.32921
-0.770033
-0.31628
-0.99081
1
200.00%
-1.07082
-1.43871
0.564417
0.295722
2
300.00%
-1.6264
0.219565
0.678805
1.88927
3
400.00%
0.961538
0.104011
-0.481165
0.850229
4
500.00%
1.45342
1.05774
0.165562
0.515018
5
600.00%
-1.33694
0.562861
1.39285
-0.063328
6
700.00%
0.121668
1.2076
-0.00204021
1.6278
7
800.00%
0.354493
1.03753
-0.385684
0.519818
8
900.00%
1.68658
-1.32596
1.42898
-2.08935
9
1000.00%
-0.12982
0.631523
-0.586538
0.29072
In [38]:
PrettyPandas(df).as_percent(subset=['A', 'B']) # Format columns A and B
Out[38]:
A
B
C
D
E
0
100.00%
132.92%
-0.770033
-0.31628
-0.99081
1
200.00%
-107.08%
-1.43871
0.564417
0.295722
2
300.00%
-162.64%
0.219565
0.678805
1.88927
3
400.00%
96.15%
0.104011
-0.481165
0.850229
4
500.00%
145.34%
1.05774
0.165562
0.515018
5
600.00%
-133.69%
0.562861
1.39285
-0.063328
6
700.00%
12.17%
1.2076
-0.00204021
1.6278
7
800.00%
35.45%
1.03753
-0.385684
0.519818
8
900.00%
168.66%
-1.32596
1.42898
-2.08935
9
1000.00%
-12.98%
0.631523
-0.586538
0.29072
In [18]:
PrettyPandas(df).as_currency('GBP')
Out[18]:
A
B
C
D
E
0
£1.00
£1.33
-£0.77
-£0.32
-£0.99
1
£2.00
-£1.07
-£1.44
£0.56
£0.30
2
£3.00
-£1.63
£0.22
£0.68
£1.89
3
£4.00
£0.96
£0.10
-£0.48
£0.85
4
£5.00
£1.45
£1.06
£0.17
£0.52
5
£6.00
-£1.34
£0.56
£1.39
-£0.06
6
£7.00
£0.12
£1.21
-£0.00
£1.63
7
£8.00
£0.35
£1.04
-£0.39
£0.52
8
£9.00
£1.69
-£1.33
£1.43
-£2.09
9
£10.00
-£0.13
£0.63
-£0.59
£0.29
In [19]:
PrettyPandas(df).as_percent(precision=3)
Out[19]:
A
B
C
D
E
0
100.000%
132.921%
-77.003%
-31.628%
-99.081%
1
200.000%
-107.082%
-143.871%
56.442%
29.572%
2
300.000%
-162.640%
21.957%
67.880%
188.927%
3
400.000%
96.154%
10.401%
-48.117%
85.023%
4
500.000%
145.342%
105.774%
16.556%
51.502%
5
600.000%
-133.694%
56.286%
139.285%
-6.333%
6
700.000%
12.167%
120.760%
-0.204%
162.780%
7
800.000%
35.449%
103.753%
-38.568%
51.982%
8
900.000%
168.658%
-132.596%
142.898%
-208.935%
9
1000.000%
-12.982%
63.152%
-58.654%
29.072%
In [20]:
PrettyPandas(df).as_currency(currency="GBP")
Out[20]:
A
B
C
D
E
0
£1.00
£1.33
-£0.77
-£0.32
-£0.99
1
£2.00
-£1.07
-£1.44
£0.56
£0.30
2
£3.00
-£1.63
£0.22
£0.68
£1.89
3
£4.00
£0.96
£0.10
-£0.48
£0.85
4
£5.00
£1.45
£1.06
£0.17
£0.52
5
£6.00
-£1.34
£0.56
£1.39
-£0.06
6
£7.00
£0.12
£1.21
-£0.00
£1.63
7
£8.00
£0.35
£1.04
-£0.39
£0.52
8
£9.00
£1.69
-£1.33
£1.43
-£2.09
9
£10.00
-£0.13
£0.63
-£0.59
£0.29
In [21]:
PrettyPandas(df).as_unit(' mol', location='suffix')
Out[21]:
A
B
C
D
E
0
1.00 mol
1.33 mol
-0.77 mol
-0.32 mol
-0.99 mol
1
2.00 mol
-1.07 mol
-1.44 mol
0.56 mol
0.30 mol
2
3.00 mol
-1.63 mol
0.22 mol
0.68 mol
1.89 mol
3
4.00 mol
0.96 mol
0.10 mol
-0.48 mol
0.85 mol
4
5.00 mol
1.45 mol
1.06 mol
0.17 mol
0.52 mol
5
6.00 mol
-1.34 mol
0.56 mol
1.39 mol
-0.06 mol
6
7.00 mol
0.12 mol
1.21 mol
-0.00 mol
1.63 mol
7
8.00 mol
0.35 mol
1.04 mol
-0.39 mol
0.52 mol
8
9.00 mol
1.69 mol
-1.33 mol
1.43 mol
-2.09 mol
9
10.00 mol
-0.13 mol
0.63 mol
-0.59 mol
0.29 mol
In [22]:
PrettyPandas(df).as_percent(subset=['B'])
Out[22]:
A
B
C
D
E
0
1
132.92%
-0.770033
-0.31628
-0.99081
1
2
-107.08%
-1.43871
0.564417
0.295722
2
3
-162.64%
0.219565
0.678805
1.88927
3
4
96.15%
0.104011
-0.481165
0.850229
4
5
145.34%
1.05774
0.165562
0.515018
5
6
-133.69%
0.562861
1.39285
-0.063328
6
7
12.17%
1.2076
-0.00204021
1.6278
7
8
35.45%
1.03753
-0.385684
0.519818
8
9
168.66%
-1.32596
1.42898
-2.08935
9
10
-12.98%
0.631523
-0.586538
0.29072
In [23]:
PrettyPandas(df).as_percent(subset=['B']).total()
Out[23]:
A
B
C
D
E
0
1
132.92%
-0.770033
-0.31628
-0.99081
1
2
-107.08%
-1.43871
0.564417
0.295722
2
3
-162.64%
0.219565
0.678805
1.88927
3
4
96.15%
0.104011
-0.481165
0.850229
4
5
145.34%
1.05774
0.165562
0.515018
5
6
-133.69%
0.562861
1.39285
-0.063328
6
7
12.17%
1.2076
-0.00204021
1.6278
7
8
35.45%
1.03753
-0.385684
0.519818
8
9
168.66%
-1.32596
1.42898
-2.08935
9
10
-12.98%
0.631523
-0.586538
0.29072
Total
55
174.29%
1.28612
2.45891
2.84508
In [65]:
(
df.pipe(PrettyPandas)
.as_percent(precision=0)
.median()
.style
.background_gradient()
)
Out[65]:
A
B
C
D
E
0
100%
133%
-77%
-32%
-99%
1
200%
-107%
-144%
56%
30%
2
300%
-163%
22%
68%
189%
3
400%
96%
10%
-48%
85%
4
500%
145%
106%
17%
52%
5
600%
-134%
56%
139%
-6%
6
700%
12%
121%
-0%
163%
7
800%
35%
104%
-39%
52%
8
900%
169%
-133%
143%
-209%
9
1000%
-13%
63%
-59%
29%
Median
550%
24%
39%
8%
41%
In [40]:
PrettyPandas(df).as_percent(subset=pd.IndexSlice[3,:], precision=2)
Out[40]:
A
B
C
D
E
0
1
1.32921
-0.770033
-0.31628
-0.99081
1
2
-1.07082
-1.43871
0.564417
0.295722
2
3
-1.6264
0.219565
0.678805
1.88927
3
400.00%
96.15%
10.40%
-48.12%
85.02%
4
5
1.45342
1.05774
0.165562
0.515018
5
6
-1.33694
0.562861
1.39285
-0.063328
6
7
0.121668
1.2076
-0.00204021
1.6278
7
8
0.354493
1.03753
-0.385684
0.519818
8
9
1.68658
-1.32596
1.42898
-2.08935
9
10
-0.12982
0.631523
-0.586538
0.29072
In [52]:
first_row_idx = pd.IndexSlice[0, :]
second_row_idx = pd.IndexSlice[1, :]
(
df.pipe(PrettyPandas)
.as_currency(subset=first_row_idx)
.as_percent(subset=second_row_idx)
.total(axis=1)
)
Out[52]:
A
B
C
D
E
Total
0
$1.00
$1.33
-$0.77
-$0.32
-$0.99
$0.25
1
200.00%
-107.08%
-143.87%
56.44%
29.57%
35.06%
2
3
-1.6264
0.219565
0.678805
1.88927
4.16124
3
4
0.961538
0.104011
-0.481165
0.850229
5.43461
4
5
1.45342
1.05774
0.165562
0.515018
8.19174
5
6
-1.33694
0.562861
1.39285
-0.063328
6.55545
6
7
0.121668
1.2076
-0.00204021
1.6278
9.95503
7
8
0.354493
1.03753
-0.385684
0.519818
9.52615
8
9
1.68658
-1.32596
1.42898
-2.08935
8.70025
9
10
-0.12982
0.631523
-0.586538
0.29072
10.2059
In [31]:
(
df
.pipe(PrettyPandas)
.as_currency('GBP', subset='A')
.as_percent(subset='B')
.total()
.average()
)
Out[31]:
A
B
C
D
E
0
£1.00
132.92%
-0.770033
-0.31628
-0.99081
1
£2.00
-107.08%
-1.43871
0.564417
0.295722
2
£3.00
-162.64%
0.219565
0.678805
1.88927
3
£4.00
96.15%
0.104011
-0.481165
0.850229
4
£5.00
145.34%
1.05774
0.165562
0.515018
5
£6.00
-133.69%
0.562861
1.39285
-0.063328
6
£7.00
12.17%
1.2076
-0.00204021
1.6278
7
£8.00
35.45%
1.03753
-0.385684
0.519818
8
£9.00
168.66%
-1.32596
1.42898
-2.08935
9
£10.00
-12.98%
0.631523
-0.586538
0.29072
Total
£55.00
174.29%
1.28612
2.45891
2.84508
Average
£5.50
17.43%
0.128612
0.245891
0.284508
In [59]:
(
df
.pipe(PrettyPandas)
.total(axis=1)
.to_frame()
)
Out[59]:
A
B
C
D
E
Total
0
1.0
1.329212
-0.770033
-0.316280
-0.990810
0.252088
1
2.0
-1.070816
-1.438713
0.564417
0.295722
0.350609
2
3.0
-1.626404
0.219565
0.678805
1.889273
4.161238
3
4.0
0.961538
0.104011
-0.481165
0.850229
5.434613
4
5.0
1.453425
1.057737
0.165562
0.515018
8.191742
5
6.0
-1.336936
0.562861
1.392855
-0.063328
6.555452
6
7.0
0.121668
1.207603
-0.002040
1.627796
9.955026
7
8.0
0.354493
1.037528
-0.385684
0.519818
9.526155
8
9.0
1.686583
-1.325963
1.428984
-2.089354
8.700249
9
10.0
-0.129820
0.631523
-0.586538
0.290720
10.205885
Content source: HHammond/PrettyPandas
Similar notebooks: